For more information, see the original guide this example is based on
# Load Packages
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
You can also embed plots, for example:
sp <- ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point(size = 3, aes(text = paste0("Species: ", Species,
"<br>",
"Sepal Width: ", Sepal.Width,
"<br>",
"Petal Width: ", Petal.Width,
"<br>",
"Sepal Length: ", Sepal.Length,
"<br>",
"Petal Length: ", Petal.Length)))
## Warning in geom_point(size = 3, aes(text = paste0("Species: ", Species, :
## Ignoring unknown aesthetics: text
ggplotly(sp, tooltip = "text") %>%
layout(hoverlabel=list(bgcolor = "#ffffff"))
# Assign filtered data frame to df
df <- txhousing %>%
filter(city == "Abilene" & year == 2000)
# Format month numbers as the month name
df$month <- month(df$month, abbr = FALSE, label = TRUE)
# Create barplot
bp <- df %>%
ggplot(aes(x = month, y = sales)) +
geom_col(stat = "identity",
fill = "#1f5673",
aes(text = paste0(
"<b>", month, " Summary", "</b>",
"<br>",
"<b>Sales: </b>", sales,
"<br>",
"<b>Median Sale Price: </b>$", median,
"<br>",
"<b>Percentage of Listings Sold: </b>", round(sales/listings*100, 2), "%"
))) +
labs(title = "Monthly House Sales for Abilene in the Year 2000",
x = "Month",
y = "Total House Sales") +
geom_text(aes(label = sales,
y = sales + 3,
text = paste0(sales, " Monthly Sales")))
## Warning in geom_col(stat = "identity", fill = "#1f5673", aes(text =
## paste0("<b>", : Ignoring unknown parameters: `stat`
## Warning in geom_col(stat = "identity", fill = "#1f5673", aes(text =
## paste0("<b>", : Ignoring unknown aesthetics: text
## Warning in geom_text(aes(label = sales, y = sales + 3, text = paste0(sales, :
## Ignoring unknown aesthetics: text
# Wrap with ggplotly()
ggplotly(bp, tooltip = "text")%>%
layout(hoverlabel=list(bgcolor = "#ffffff"))
That’s it! Animating your ggplot2 graphs with plotly isn’t overly complex and doesn’t require you to learn an entire package. There are some nuances as you’ll noticed with how I positioned the geom_text() in the house sales graph. I typically wrap the majority of my ggplot2 graphs in ggplotly() since improve the user experience. Not only can they glance at the graph to get basic information, but simply hovering over a data point their interested in will provide them with additional information, without navigating away from the graph.